package com.android.jingweiapp.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.fragment.interaction.ConsultFragment;
import com.android.jingweiapp.fragment.interaction.MailFragment;
import com.android.jingweiapp.fragment.interaction.OnlineJudgeFragment;
import com.android.jingweiapp.fragment.interaction.PolicyFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 互动
 */
public class InterctFragment extends Fragment {

    @Bind(R.id.tab_interaction_0)
    TextView tabInteraction0;
    @Bind(R.id.tab_interaction_1)
    TextView tabInteraction1;
    @Bind(R.id.tab_interaction_2)
    TextView tabInteraction2;
    @Bind(R.id.tab_interaction_3)
    TextView tabInteraction3;

    public InterctFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_interction, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        TextView[] textViews = new TextView[]{
                tabInteraction0,
                tabInteraction1,
                tabInteraction2,
                tabInteraction3,
        };
        Fragment[] fragments = new Fragment[]{
                new MailFragment(),
                new ConsultFragment(),
                new OnlineJudgeFragment(),
                new PolicyFragment()
        };

        getChildFragmentManager().beginTransaction()
                .replace(R.id.ly_interaction_container, fragments[0])
                .commit();

        textViews[0].setSelected(true);
        setClick(textViews, fragments);
    }

    private void setClick(final TextView[] textViews, final Fragment[] fragments) {
        for (int i = 0; i < textViews.length; i++) {
            final int finalI = i;
            textViews[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getChildFragmentManager().beginTransaction()
                            .replace(R.id.ly_interaction_container, fragments[finalI])
                            .commit();
                    for (int j = 0; j < textViews.length; j++) {
                        if (j == finalI) {
                            textViews[j].setSelected(true);
                        } else {
                            textViews[j].setSelected(false);
                        }
                    }
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
